package cn.edu.lnpu.manage.cms.client.mq;

import cn.edu.lnpu.domain.cms.CmsPage;
import cn.edu.lnpu.manage.cms.client.dao.CmsPageRepository;
import cn.edu.lnpu.manage.cms.client.service.PageService;
import com.alibaba.fastjson.JSON;
import lombok.extern.java.Log;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

@Log
@Component
public class ConsumerPostPage {

    @Autowired
    CmsPageRepository cmsPageRepository;
    @Autowired
    PageService pageService;

    @RabbitListener(queues={"${lnpu.mq.queue}"})
    public void postPage(String msg){
        //解析消息
        Map map = JSON.parseObject(msg, Map.class);
        log.info("receive cms post page:"+msg.toString());
        //取出页面id
        String pageId = (String) map.get("pageId");
        //查询页面信息
        Optional<CmsPage> optional = cmsPageRepository.findById(pageId);
        if(!optional.isPresent()){
            log.info("receive cms post page,cmsPage is null:"+msg.toString());
            return ;
        }
        //将页面保存到服务器物理路径
        pageService.savePageToServerPath(pageId);
    }
}
